package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.dai
 * @Date 2024/12/3 上午10:12
 */
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "地址")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;

    /*地址新增*/
    @PostMapping
    public void add(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.add(addressBookUpsertReqDTO);
    }

    /*地址分页查询*/
    @GetMapping("/page")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return addressBookService.page1(addressBookPageQueryReqDTO);
    }

    /*地址薄详情*/
    @GetMapping("/{id}")
    public AddressBookResDTO detail(@NotNull(message = "id不能为空") @PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    /*地址薄修改*/
    @PutMapping("/{id}")
    public void update(@NotNull(message = "id不能为空") @PathVariable("id") Long id,
                       @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.update(id, addressBookUpsertReqDTO);
    }

    /*地址蒲删除*/
    @DeleteMapping("/batch")
    public void logicallyDelete(@NotNull(message = "id列表不能为空") @RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
    }

    /*设置默认地址*/
    @PutMapping("/default")
    public void updateDefaultStatus(@NotNull(message = "id不能为空") @RequestParam("id") Long id,
                                    @NotNull(message = "状态值不能为空") @RequestParam("flag") Integer flag) {
        Long userId = UserContext.currentUserId();
        addressBookService.updateDefaultStatus(userId, id, flag);
    }

    /*获取默认地址*/
    @GetMapping("/defaultAddress")
    public AddressBookResDTO defaultAddress() {
        return addressBookService.defaultAddress();
    }
}
